package com.atguigu.campusintelligentassistantsystem.controller;

import com.atguigu.campusintelligentassistantsystem.entity.DormRepair;
import com.atguigu.campusintelligentassistantsystem.service.DormRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dorm-repair")
@CrossOrigin(origins = "*")
public class DormRepairController {

    @Autowired
    private DormRepairService dormRepairService;

    /**
     * 提交报修申请
     * @param dormRepair 报修信息
     * @return 保存后的报修单
     */
    @PostMapping
    public ResponseEntity<DormRepair> submitRepairRequest(@RequestBody DormRepair dormRepair) {
        DormRepair savedRepair = dormRepairService.submitRepairRequest(dormRepair);
        return ResponseEntity.ok(savedRepair);
    }

    /**
     * 根据ID获取报修单
     * @param id 报修单ID
     * @return 报修单信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<DormRepair> getRepairById(@PathVariable Long id) {
        DormRepair repair = dormRepairService.getRepairById(id);
        if (repair != null) {
            return ResponseEntity.ok(repair);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据学生ID获取报修记录
     * @param studentId 学生ID
     * @return 报修记录列表
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<DormRepair>> getRepairsByStudentId(@PathVariable Long studentId) {
        List<DormRepair> repairs = dormRepairService.getRepairsByStudentId(studentId);
        return ResponseEntity.ok(repairs);
    }

    /**
     * 获取所有报修单
     * @return 报修单列表
     */
    @GetMapping
    public ResponseEntity<List<DormRepair>> getAllRepairs() {
        List<DormRepair> repairs = dormRepairService.getAllRepairs();
        return ResponseEntity.ok(repairs);
    }

    /**
     * 更新报修单状态
     * @param id 报修单ID
     * @param status 状态
     * @return 更新后的报修单
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<DormRepair> updateRepairStatus(@PathVariable Long id, @RequestParam String status) {
        DormRepair updatedRepair = dormRepairService.updateRepairStatus(id, status);
        if (updatedRepair != null) {
            return ResponseEntity.ok(updatedRepair);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 删除报修单
     * @param id 报修单ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteRepair(@PathVariable Long id) {
        dormRepairService.deleteRepair(id);
        return ResponseEntity.ok("报修单删除成功");
    }
}